package com.example.templateapp.ui.fragment.dialog

import android.app.AlertDialog
import android.app.Dialog
import android.os.Bundle
import android.view.LayoutInflater
import android.view.WindowManager
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.fragment.app.DialogFragment
import com.example.templateapp.R
import com.example.templateapp.databinding.DialogFragmentWebViewBinding

class WebDialogFragment(val url: String?) : DialogFragment() {

    lateinit var dialog: AlertDialog

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {

        val dialogBuilder = AlertDialog.Builder(requireContext())
        dialogBuilder.setIcon(R.mipmap.ic_launcher_round)
        dialogBuilder.setTitle("通知")
        dialogBuilder.setPositiveButton(
            "确认"
        ) { dialog, which ->
            dialog.dismiss()
        }

        val binding = DialogFragmentWebViewBinding.inflate(LayoutInflater.from(requireContext()))
        dialogBuilder.setView(binding.root)
        val web = binding.web
        initWebView(web)

        dialog = dialogBuilder.create()

        val lp = dialog.window?.attributes
        lp?.height = WindowManager.LayoutParams.MATCH_PARENT
        dialog.window?.attributes = lp
        return dialog
    }

    private fun initWebView(web: WebView) {
        val setting = web.settings
        setting.loadWithOverviewMode = true


        val data = "<!DOCTYPE html>\n" +
                "<html>\n" +
                " <head>\n" +
                "  <meta charset=\"UTF-8\">\n" +
                "  <title></title>\n" +
                " </head>\n" +
                " <body>\n" +
                "  司机师傅您好：感谢您对敬业物流事业做出的支持与贡献，以下为保卫科测评链接，欢迎您对我们的工作进行测评：<a href=\" \">http://www.diaochapai.com/survey3079239</a >\n" +
                " </body>\n" +
                "</html>"

/*        web.settings.loadWithOverviewMode = true
        web.settings.domStorageEnabled = true*/
        web.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(
                view: WebView?,
                request: WebResourceRequest?
            ): Boolean {
                return false
            }

            override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
                return false
            }
        }
        web.loadData(data, "text/html; charset=UTF-8", null)
//        web.loadUrl(url)
    }

}